﻿@page "/Admin/Feed"
@using Moments.Model
@using Moments.Service
@inject IFreeSql Db
@inject GatherService GatherService

<MCard Color="white lighten-4 mb-2" Style="border-radius: 12px!important;padding: 6px 0;" Elevation="0"
       Flat
       Tile>
    <MToolbar Dense Elevation="0">
        <MButton class="ma-2 white--text"
                 Color="blue-grey"
                 OnClick="DelAll">
            清空数据
            <MIcon Right Dark>
                fa:fas fa-trash
            </MIcon>
        </MButton>
        <MButton class="ma-2 white--text"
                 Color="blue-grey"
                 OnClick="Gather">
            一键采集
            <MIcon Right Dark>
                fa:fas fa-feather
            </MIcon>
        </MButton>
        <MSpacer></MSpacer>
    </MToolbar>
</MCard>

<MList Subheader
       TwoLine Elevation="0" Style="border-radius: 20px">
    @foreach (var item in _nodeList)
    {
        <MListItem OnClick="() => { _now = item;_dialog = true; }">
            <MListItemAvatar>
                <MImage Src="@GetAvatar(item.FriendId)"> </MImage>
            </MListItemAvatar>
            <MListItemContent>
                <MListItemTitle>@item.Title</MListItemTitle>
                <MListItemSubtitle>@item.Description</MListItemSubtitle>
            </MListItemContent>
            <MListItemAction>
                <MButton Icon>
                    <MIcon Color="grey lighten-1">mdi:mdi-information</MIcon>
                </MButton>
            </MListItemAction>
        </MListItem>
    }
</MList>
<div class="text-center" style="margin: 15px">
    <MPagination ValueChanged="async Task(v) => { await PutPage(v); }"
                 Length="_maxP"
                 TotalVisible="7">
    </MPagination>
</div>
<MRow Justify="JustifyTypes.Center">
    <MDialog @bind-Value="_dialog"
             Scrollable
             MaxWidth="980">
        <ChildContent>
            <MCard>
                    <MToolbar Dark
                              Color="primary">
                        <MButton Icon
                                 Dark
                                 OnClick="() => _dialog = false">
                            <MIcon>mdi:mdi-close</MIcon>
                        </MButton>
                        <MToolbarTitle>@_now.Title</MToolbarTitle>
                        <MSpacer></MSpacer>
                        <MToolbarItems>
                            <MButton Target="_blank" Link Href="@_now.Link">
                                查看原文
                            </MButton>
                        </MToolbarItems>
                    </MToolbar>
                <MCardText Class="markdown-body p-4" Style="height: 800px;padding-top: 20px">
                    @(new MarkupString((_now.Content ?? _now.Description) ?? string.Empty))
                </MCardText >
            </MCard>
        </ChildContent>
    </MDialog>
</MRow>

@code {
    List<Article> _nodeList = new();
    Article _now = new();
    long _count;
    int _maxP;
    private bool _dialog;
    List<Friend> _friends = new();


    protected override async Task OnInitializedAsync()
    {
        _count = await Db.Select<Article>().CountAsync();
        _maxP = (int)Math.Ceiling(_count / 10.0);
        _nodeList = await Db.Select<Article>().OrderBy("PubDate DESC")
            .Limit(10)
            .ToListAsync();
        _friends = await Db.Select<Friend>().ToListAsync();
    }

    string GetAvatar(int? id)
    {
        return _friends.Find(x => x.FriendId == id)?.Avatar ?? "https://gravatar.loli.net/avatar/c243c9fe3d177bc3f656ff4554e4cbb0?s=100&d=mm&r=g";
    }

    async Task PutPage(int p)
    {
        if (p < 1)
        {
            p = 1;
        }
        if (p > _maxP)
        {
            p = _maxP;
        }
        _nodeList = await Db.Select<Article>().OrderBy("PubDate DESC")
            .Offset((p - 1) * 10)
            .Limit(10)
            .ToListAsync();
        StateHasChanged();
    }

    async Task DelAll()
    {
        await Db.Delete<Article>()
            .Where("1=1")
            .ExecuteAffrowsAsync();
        AdminLayout.PushSnackbars(AlertTypes.Info,"清空成功！");
        _nodeList.Clear();
    }

     void Gather()
    {
        GatherService.GatherRssAll();
        AdminLayout.PushSnackbars(AlertTypes.Info,"采集任务启动成功，请稍等一会刷新本页面！");

    }

}